home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / SPHERES.ZIP / SPHERES.CPP < prev    next >
C/C++ Source or Header  |  1993-10-21  |  8KB  |  315 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2. //
  3. // spheres.cpp
  4. //
  5. // Modified by Philip VanBaren (71214,2302 or phillipv@engin.umich.edu)
  6. // to enable the screen saver to work with NDW's Sleeper program.
  7. //
  8. // The main modifications are:
  9. //   1) Window class is "WindowsScreenSaverClass"  (this is required for the
  10. //        saver to work properly with Sleeper.)
  11. //   2) IdleAction does screen saver things on only 1 out of 100 calls,
  12. //        thereby allowing background events (backups, printing, etc.) to
  13. //        retain most of the processing power when they need it.
  14. //   3) "-c", "/c", "c", "-s", "/s", "s", and "" are accepted as valid command
  15. //        line parameters.  (Sleeper passes "c" and "-s" to start the configure
  16. //        and saver routines, respectively.)
  17. //   4) Allow some slop in mouse movement before aborting the screen saver,
  18. //        so that the screen saver doesn't abort whenever the desk is bumped.
  19.  
  20. #include <owl.h>
  21. #include <dialog.h>
  22. #include <bwcc.h>
  23. #include <math.h>
  24. #include <stdio.h>
  25. #include "tscrnsav.h"
  26. #include "spheres.h"
  27.  
  28. char szAppName[] = "ScreenSaver.Spheres";
  29. int max_spheres;
  30.  
  31. HDC hDC;
  32. HPALETTE hPalette,hpaletteold;
  33. int grayscale;
  34.  
  35. #define PALETTE_SIZE 200
  36.  
  37. LOGPALETTE *plgpl;    /* address of LOGPALETTE structure    */
  38.  
  39. _CLASSDEF( TMyScrnSavWindow )
  40. class TMyScrnSavWindow : public TScrnSavWindow
  41. {
  42.    int xlen,ylen;
  43.    int i;
  44.    int radius;
  45.    int cx,cy;
  46.    int red,green,blue;
  47.    int spheres;
  48.    double x_offset,y_offset;
  49.    HPEN hpen;
  50. public:
  51.    TMyScrnSavWindow( PTWindowsObject AParent, LPSTR ATitle,
  52.                      PTModule AModule = NULL );
  53.    ~TMyScrnSavWindow();
  54.    virtual void  GetWindowClass( WNDCLASS & AWndClass );
  55.    virtual void  AnimateScreen();
  56. };
  57.  
  58. TMyScrnSavWindow::TMyScrnSavWindow( PTWindowsObject AParent,
  59.                                     LPSTR ATitle,
  60.                                     PTModule AModule ) :
  61. TScrnSavWindow( AParent, ATitle, AModule )
  62. {
  63.     randomize();
  64.     xlen = GetSystemMetrics( SM_CXSCREEN );
  65.     ylen = GetSystemMetrics( SM_CYSCREEN );
  66.     i=0;
  67.     radius=0;
  68.     spheres=max_spheres;
  69. }
  70.  
  71. TMyScrnSavWindow::~TMyScrnSavWindow()
  72. {
  73.    if(grayscale && hPalette)
  74.    {
  75.       SelectPalette(hDC,hpaletteold,0);
  76.       DeleteObject(hPalette);
  77.    }
  78.    ReleaseDC( HWindow, hDC );
  79. }
  80.  
  81. void TMyScrnSavWindow::GetWindowClass( WNDCLASS & AWndClass )
  82. {
  83.     TScrnSavWindow::GetWindowClass( AWndClass );
  84.     AWndClass.hbrBackground = ( HBRUSH )GetStockObject( BLACK_BRUSH );
  85.     AWndClass.style |= CS_OWNDC;
  86. }
  87.  
  88. void TMyScrnSavWindow::AnimateScreen()
  89. {
  90.    HBRUSH hbrush,hbrushold;
  91.    int x1,y1,x2,y2;
  92.    double ci;
  93.    int c1,c2;
  94.    static int setup=1;
  95.  
  96.    if(setup)
  97.    {
  98.       hDC = GetDC( HWindow );
  99.       hpen = (HPEN)GetStockObject(NULL_PEN);
  100.  
  101.       /*
  102.        *  Initialize the palette if in grayscale mode.
  103.        */
  104.       if(grayscale)
  105.       {
  106.          plgpl = (LOGPALETTE*) LocalAlloc(LPTR,sizeof(LOGPALETTE)
  107.                                     + PALETTE_SIZE * sizeof(PALETTEENTRY));
  108.  
  109.          plgpl->palNumEntries = PALETTE_SIZE;
  110.          plgpl->palVersion = 0x300;
  111.  
  112.          for(int k=0;k<PALETTE_SIZE;k++)
  113.          {
  114.             plgpl->palPalEntry[k].peRed =   256-PALETTE_SIZE+k;
  115.             plgpl->palPalEntry[k].peGreen = 256-PALETTE_SIZE+k;
  116.             plgpl->palPalEntry[k].peBlue =  256-PALETTE_SIZE+k;
  117.             plgpl->palPalEntry[k].peFlags = NULL;
  118.          }
  119.  
  120.          hPalette = CreatePalette(plgpl);
  121.          LocalFree((HLOCAL) plgpl);
  122.  
  123.          hpaletteold=SelectPalette(hDC,hPalette,0);
  124.  
  125.          RealizePalette(hDC);
  126.       }
  127.       setup=0;
  128.    }
  129.  
  130.    if ( hDC )
  131.    {
  132.       if(i<(radius-1))
  133.       {
  134.          i+=2;
  135.  
  136.          x1=cx-radius+(i*x_offset);
  137.          x2=x1+2*(radius-i);
  138.          y1=cy-radius+(i*y_offset);
  139.          y2=y1+2*(radius-i);
  140.  
  141.          ci=sin((double)i/radius);
  142.  
  143.          if(grayscale)
  144.          {
  145.             hbrush=CreateSolidBrush(PALETTEINDEX(floor(ci*(PALETTE_SIZE-0.5))));
  146.          }
  147.          else
  148.          {         
  149.             ci=ci*1.1+0.2;
  150.             if(ci<1)
  151.             {
  152.                hbrush = CreateSolidBrush(RGB(ci*red,ci*green,ci*blue));
  153.             }
  154.             else
  155.             {
  156.                ci=(ci-1)/0.3;
  157.                hbrush = CreateSolidBrush(RGB(red+ci*(255-red),green+ci*(255-green),blue+ci*(255-blue)));
  158.             }
  159.          }
  160.          hbrushold = (HBRUSH)SelectObject(hDC,hbrush);
  161.          SelectObject(hDC,hpen);
  162.  
  163.          Ellipse(hDC,x1,y1,x2,y2);
  164.  
  165.          SelectObject(hDC,hbrushold);
  166.          DeleteObject(hbrush);
  167.       }
  168.       else
  169.       {
  170.          i=0;
  171.          spheres++;
  172.          if(spheres>=max_spheres)
  173.          {
  174.             InvalidateRect(HWindow,NULL,TRUE);
  175.             spheres=0;
  176.             /*
  177.              *  Compute a random "light source direction"
  178.              */
  179.             x_offset=(double)random(1000)/1000+0.5;
  180.             y_offset=(double)random(1000)/1000+0.5;
  181.          }
  182.          /*
  183.           *  Compute a random radius and color
  184.           */
  185.          radius=random(xlen/10)+xlen/30;
  186.          cx=random(xlen-2*radius)+radius;
  187.          cy=random(ylen-2*radius)+radius;
  188.  
  189.          c1=random(6);
  190.          c2=random(255);
  191.          if(c1==0)
  192.          {
  193.             red=c2;
  194.             green=0;
  195.             blue=255;
  196.          }
  197.          else if(c1==1)
  198.          {
  199.             red=c2;
  200.             green=255;
  201.             blue=0;
  202.          }
  203.          else if(c1==2)
  204.          {
  205.             red=255;
  206.             green=c2;
  207.             blue=0;
  208.          }
  209.          else if(c1==3)
  210.          {
  211.             red=0;
  212.             green=c2;
  213.             blue=255;
  214.          }
  215.          else if(c1==4)
  216.          {
  217.             red=0;
  218.             green=255;
  219.             blue=c2;
  220.          }
  221.          else
  222.          {
  223.             red=255;
  224.             green=0;
  225.             blue=c2;
  226.          }
  227.       }
  228.    }
  229. }
  230.  
  231.  
  232. _CLASSDEF( TMyScrnSavDlg )
  233. class TScrnSavDlg : public TDialog
  234. {
  235. public:
  236.    TScrnSavDlg( PTWindowsObject AParent,
  237.                 LPSTR AName,
  238.                 PTModule AModule = NULL ) : TDialog( AParent, AName, AModule )
  239.                 {}
  240.    void WMInitDialog(TMessage&);
  241.    void Ok(TMessage&) = [ID_FIRST + IDOK];
  242.    void Cancel(TMessage&) = [ID_FIRST + IDCANCEL];
  243. };
  244.  
  245. void TScrnSavDlg::WMInitDialog(TMessage&)
  246. {
  247.    SetDlgItemInt(HWindow,IDD_MAXSPHERES,max_spheres,FALSE);
  248.    CheckDlgButton(HWindow,IDD_GRAYSCALE,grayscale);
  249. }
  250.  
  251. void TScrnSavDlg::Ok(TMessage&)
  252. {
  253.    char ach[20];
  254.    GetDlgItemText(HWindow,IDD_MAXSPHERES,ach,20);
  255.    max_spheres=atoi(ach);
  256.    grayscale=IsDlgButtonChecked(HWindow,IDD_GRAYSCALE);
  257.  
  258.    if(max_spheres<1) max_spheres=1;
  259.    wsprintf(ach,"%d",max_spheres);
  260.    WritePrivateProfileString("Screen Saver.Spheres","Spheres",ach,"CONTROL.INI");
  261.    wsprintf(ach,"%d",grayscale);
  262.    WritePrivateProfileString("Screen Saver.Spheres","Grayscale",ach,"CONTROL.INI");
  263.  
  264.    SendMessage(HWindow,WM_SYSCOMMAND,SC_CLOSE,0L);
  265. }
  266.  
  267. void TScrnSavDlg::Cancel(TMessage&)
  268. {
  269.    SendMessage(HWindow,WM_SYSCOMMAND,SC_CLOSE,0L);
  270. }
  271.  
  272.  
  273. _CLASSDEF( TMyScrnSavApp )
  274. class TMyScrnSavApp : public TScrnSavApp
  275. {
  276.     public:
  277.         TMyScrnSavApp( LPSTR AName, HINSTANCE AnInstance,
  278.                        HINSTANCE APrevInstance,
  279.                        LPSTR ACmdLine, int ACmdShow ) :
  280.         TScrnSavApp( AName, AnInstance, APrevInstance, ACmdLine, ACmdShow )
  281.         {
  282.            max_spheres=GetPrivateProfileInt("Screen Saver.Spheres","Spheres",50,"CONTROL.INI");
  283.            if(max_spheres<1) max_spheres=1;
  284.            grayscale=GetPrivateProfileInt("Screen Saver.Spheres","Grayscale",0,"CONTROL.INI");
  285.         }
  286.         virtual void  InitScrnSavWindow();
  287.         virtual void  InitConfigDialog();
  288. };
  289.  
  290.  
  291. void TMyScrnSavApp::InitScrnSavWindow()
  292. {
  293.     pScrnSavWnd = new TMyScrnSavWindow( NULL, szAppName );
  294. }
  295.  
  296.  
  297. void TMyScrnSavApp::InitConfigDialog()
  298. {
  299.    /*
  300.     *  Force BWCC.DLL to be linked.
  301.     */
  302.    BWCCGetVersion();
  303.    pConfigureDialog = new TScrnSavDlg( NULL, "CONFIGUREDIALOG" );
  304. }
  305.  
  306.  
  307. int PASCAL WinMain( HINS